<template>
  <div id="login">
    <h1>This is an login page</h1>
    <div>
      <p>
        用户名：
        <input v-model="name" />
      </p>
      <p>
        密码：
        <input v-model="pass" />
      </p>
      <button @click="login">登录</button>
      跳转地址:{{rpath}}
    </div>
  </div>
</template>
<script>
import utils from "../../utils/utils";
export default {
  data() {
    return {
      name: "mm",
      pass: "8888",
      okToken: null,
      rpath: null
    };
  },
  components: {},
  mounted: function() {
    if (this.$route.query.redirect) {
      this.rpath = this.$route.query.redirect;
    }
  },
  methods: {
    login: function() {
      const _this = this;

      this.$axios
        .post("./api/login2", {
          username: this.name,
          password: this.pass
        })
        .then(function(response) {
          console.log("拿到token");
          if (response.data.code == "200") {
            //debugger;
            let infos = response.data.result;
            if (infos != null) {
              localStorage.setItem("token", infos.token);
              if (infos.cookies) {
                var cookiess = infos.cookies;
                console.log("进入cookies...循环添加。。。");
                sessionStorage.setItem('mycookies',JSON.stringify(cookiess) );
                for (let k in cookiess) {
                  console.log(k, cookiess[k]);
                  utils.setCookie(k, cookiess[k], 1296000);
                }
                //utils.setCookie("nickname",res.data.result.user_name,1296000)
                
              }
            }

            //设置半个月
            //utils.setCookie("nickname", response.data.result.nickname, 21600);

            if (_this.rpath != null) {
              console.log("跳转到repath...");
              if (_this.rpath.indexOf("/ready") != -1) {
                _this.rpath = utils.getUrl(_this.rpath);
              }
              /*
window.location.href =
                utils.getUrl(_this.rpath) +
                "ready?token=" +
                response.data.result.token +
                "&redirect=" +
                _this.rpath;
              */
              window.location.href = _this.rpath;
            } else {
              window.location.href = "/";
            }
          }
          //console.log(response.data)
          //console.log(response.data.result.token)
        });
    }
  }
};
</script>